package com.mmy.fta.trade.server.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;

/**
 * @author qingyu.meng
 * @date 2021年10月25日 10:40 上午
 * @Description
 */
@Slf4j
public class SmsUtil {
    private static final String accessKeyId = "LTAI4GCjWSbTHQzGTaavFHKU";
    private static final String accessKeySecret = "sDiW0PSXaAKXfNfwCI8vaG4spE4CUc";
    private static final String signName = "九次方社交微服务";
    private static final String templateCode = "SMS_193247722";

    /**
     * 阿里云发送短信验证码
     *
     * @param mobile    用户手机号
     * @param msg 信息
     */
    public static void sendSms(String mobile, String msg) {
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        //这里使用通配符，code要与在阿里云注册的模版相同
        request.putQueryParameter("TemplateParam", "{\"code\":" + msg + "}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info("阿里云短信response：{}",response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

}
